import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { PetIlluEventName } from "../view/pet/illu/PetIlluEventName";
import { IView } from "../../mvc/interfaces/IView";
import { PetIllustrationModule } from "../../../modules/PetIllustrationModule";
import { DataEvent } from "../events/DataEvent";
import { SPetInfo } from "../../../Message/Game/SPetInfo";
import { PetConfig } from "../resource/PetConfig";
import { PetIlluNewWindow } from "../view/pet/illu/PetIlluNewWindow";
import { IPetIllustrationModule } from "../../../modules/interfaces/IPetIllustrationModule";
type int = number;
//class PetIllustrationController
    
    export  class PetIllustrationController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.PetIlluOpenWindow, this.onPetIlluOpenWindowHandler);
            Dispatcher.addEventListener(PetIlluEventName.GotoPetPageIndex, this.onGotoPetPageIndexHandler);
            Dispatcher.addEventListener(PetIlluEventName.DealGetNewPet, this.onDealGetNewPetHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._illuModule == null) 
            {
                this._illuModule = new PetIllustrationModule();
            }
            return this._illuModule;
        }

        protected /* override */ initServer(): void
        {
            return;
        }

        /* internal  */onPetIlluOpenWindowHandler(arg1: DataEvent): void
        {
            this.initView().show();
            return;
        }

        /* internal  */onGotoPetPageIndexHandler(arg1: DataEvent): void
        {
            this.initView().show();
            if (this.sleepTimer == null) 
            {
                this.sleepTimer = new Object /* flash.utils.Timer */(100);
                this.sleepTimer.repeatCount = 1;
                this.sleepTimer.addEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.onSleepCompleteHandler);
            }
            this.sleepTimer.reset();
            this.sleepTimer.start();
            this.petCode = arg1.data as int;
            return;
        }

        /* internal  */onSleepCompleteHandler(arg1: Object /* flash.events.Event */): void
        {
            this._illuModule.showToPetPage(this.petCode);
            this.sleepTimer.stop();
            return;
        }

        /* internal  */onDealGetNewPetHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=false;
            var loc1=arg1.data as SPetInfo;
            if (loc1) 
            {
                loc2 = PetConfig.instance.getInfoByCode(loc1.code);
                if (loc2) 
                {
                    if (cache.pet.collectedPets) 
                    {
                        loc3 = !(cache.pet.collectedPets.indexOf(loc2.effect) == -1);
                    }
                }
                if (!loc3) 
                {
                    cache.pet.collectedPets.push(loc2.effect);
                    if (this._illuNewWindow == null) 
                    {
                        this._illuNewWindow = new PetIlluNewWindow();
                    }
                    this._illuNewWindow.updateByPet(loc1);
                    this._illuNewWindow.show();
                }
            }
            return;
        }

        private /* var */_illuModule: IPetIllustrationModule;

        private /* var */_illuNewWindow: PetIlluNewWindow;

        private /* var */sleepTimer: Object /* flash.utils.Timer */;

        private /* var */petCode: int;
    }
